package br.edu.sistemaacademico.builder;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Aluno;
import br.edu.sistemaacademico.pojo.PrototypeFactory;

public class AlunoDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"codigo", "nome", "cpf", "endereco", "telefone", "sexo", "rg",
			"nascimento", "anoIngresso", "coeficiente", "login", "senha",
			"status" });

	public List<Object> getCampos() {
		return (List) COLUNAS;
	}

	@Override
	public void alterar(Object o) throws SQLException {
		Aluno c = (Aluno) o;
		DataFacade facade = new DataFacade(c.getNascimento());
		List<Object> valores = Arrays
				.asList(new Object[] { c.getId(), c.getNome(), c.getCpf(),
						c.getEndereco(), c.getTelefone(), c.getSexo(),
						c.getRg(), facade.getStingGravar(facade.getDate()), c.getAnoIngresso(),
						c.getCoeficiente(), c.getLogin(), c.getSenha(),
						c.getStatus() });

		String condicao = "codigo = " + c.getId() + " and status = 1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_Aluno");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) {
		Aluno a = (Aluno) PrototypeFactory.createPrototypeFactory("aluno");

		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Aluno a" + tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao + "and status =1 ");
		select.colunasToReturn = "a.*".toString();

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				a = (Aluno) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return a;
	}

	public List<Object> getAll(String condicao, String tabelasAdicionais,
			String ordenacao) {
		Aluno a = (Aluno) PrototypeFactory.createPrototypeFactory("aluno");
		List<Aluno> list = new LinkedList<Aluno>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Aluno a" + tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.colunasToReturn = "a.*".toString();
		select.setCondicao(condicao + " and status =1");
		select.orderBy = ordenacao;

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			while (rs.next()) {
				list.add((Aluno) informacoes(rs));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		Aluno c = (Aluno) o;
		try {
			DataFacade facade = new DataFacade(c.getNascimento());

			List<Object> valores = Arrays.asList(new Object[] { c.getId(),
					c.getNome(), c.getCpf(), c.getEndereco(), c.getTelefone(),
					c.getSexo(), c.getRg(),
					facade.getStingGravar(facade.getDate()),
					c.getAnoIngresso(), c.getCoeficiente(), c.getLogin(),
					c.getSenha(), 1 });

			SQLDirector dir = new SQLDirector();
			InsertSQLBuilder insert = new InsertSQLBuilder();

			insert.setNomeTabela("tb_Aluno");
			insert.setColunas(COLUNAS);
			insert.setValores(valores);

			dir.buildSQL(insert);
			System.out.println(insert.getSQL());

			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		DataFacade facade = new DataFacade();
		facade.setDataVisao(rs.getString(COLUNAS.get(7)));
		facade.setDataVisao(rs.getString(COLUNAS.get(7)));
		Aluno a = (Aluno) PrototypeFactory.createPrototypeFactory("aluno");
		a.setId(rs.getLong(COLUNAS.get(0)));
		a.setNome(rs.getString(COLUNAS.get(1)));
		a.setCpf(rs.getString(COLUNAS.get(2)));
		a.setEndereco(rs.getString(COLUNAS.get(3)));
		a.setTelefone(rs.getString(COLUNAS.get(4)));
		a.setSexo(rs.getString(COLUNAS.get(5)).charAt(0));
		a.setRg(rs.getString(COLUNAS.get(6)));
		a.setNascimento(facade.getString(facade.getDate()));
		a.setAnoIngresso(rs.getInt(COLUNAS.get(8)));
		a.setCoeficiente(rs.getFloat(COLUNAS.get(9)));
		a.setLogin(rs.getString(COLUNAS.get(10)));
		a.setSenha(rs.getString(COLUNAS.get(11)));
		a.setStatus(rs.getInt(COLUNAS.get(12)));
		return a;
	}

	@Override
	public void remover(Object o) {
		Aluno c = (Aluno) o;
		List<Object> valores = Arrays
				.asList(new Object[] { c.getId(), c.getNome(), c.getCpf(),
						c.getEndereco(), c.getTelefone(), c.getSexo(),
						c.getRg(), c.getNascimento(), c.getAnoIngresso(),
						c.getCoeficiente(), c.getLogin(), c.getSenha(),
						c.getStatus() });

		// String condicao = "cpf = '" + c.getCpf() + "'";
		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_Aluno");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		// delete.setCondicao(condicao);

		dir.buildSQL(delete);
		try {
			System.out.println(delete.getSQL());
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deletar(Object o) {
		Aluno c = (Aluno) o;
		DataFacade facade = new DataFacade(c.getNascimento());
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getNome(), c.getCpf(), c.getEndereco(), c.getTelefone(),
				c.getSexo(), c.getRg(), facade.getStingGravar(facade.getDate()), c.getAnoIngresso(),
				c.getCoeficiente(), c.getLogin(), c.getSenha(), 0 });

		String condicao = "codigo = " + c.getId();// +" and status = 1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_Aluno");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
